//
//  CommFunc.m
//  CommunityNannyO2O
//
//  Created by apple on 15/8/17.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "CommFunc.h"
#import "UIViewController+Create.h"
#import "GTMBase64.h"
#import "Reachability.h"

#define KAccont @"account"

@implementation CommFunc


+ (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font max:(CGSize)maxSize{
    
    NSDictionary *dict = @{NSFontAttributeName:font};
    
    CGSize textSize =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    
    return textSize ;
}

+ (NSString*)getImageBase64String:(UIImage *)image
{
    NSData *data = nil;
    
//    //判断图片是不是png格式的文件
//    if (UIImagePNGRepresentation(image))
//    {    //返回为png图像。
//        data = UIImagePNGRepresentation(image);
//    }
//    else
//    {
//        //返回为JPEG图像。
        data = UIImageJPEGRepresentation(image, 0.75);
//    }
    
    return [data base64EncodedStringWithOptions:0];
}

+ (NSData *)getDecodedDataFromBase64String:(NSString *)base64String
{
    return [GTMBase64 decodeString:base64String];
}

+ (BOOL)validateIDCardNumber:(NSString *)value{
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    int length =0;
    if (!value) {
        return NO;
    }else {
        length = value.length;
        
        if (length !=15 && length !=18) {
            return NO;
        }
    }
    // 省份代码
    NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
    
    NSString *valueStart2 = [value substringToIndex:2];
    BOOL areaFlag =NO;
    for (NSString *areaCode in areasArray) {
        if ([areaCode isEqualToString:valueStart2]) {
            areaFlag =YES;
            break;
        }
    }
    
    if (!areaFlag) {
        return NO;
    }
    
    
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    
    int year =0;
    switch (length) {
        case 15:
            year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
            
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                              options:NSMatchingReportProgress
                                                                range:NSMakeRange(0, value.length)];
            
            //[regularExpression release];
            
            if(numberofMatch >0) {
                return YES;
            }else {
                return NO;
            }
        case 18:
            
            year = [value substringWithRange:NSMakeRange(6,4)].intValue;
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:value
                                                              options:NSMatchingReportProgress
                                                                range:NSMakeRange(0, value.length)];
            
            //[regularExpression release];
            
            if(numberofMatch >0)
            {
                int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                int Y = S %11;
                NSString *M =@"F";
                NSString *JYM =@"10X98765432";
                M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                    return YES;// 检测ID的校验位
                }else {
                    return NO;
                }
                
            }
            else
            {
                return NO;
            }
        default:
            return NO;
    }
}

//纯数字验证
+ (BOOL)isPureInt:(NSString*)string
{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

/*手机号码验证 MODIFIED BY HELENSONG*/
+ (BOOL)isValidateMobile:(NSString *)mobile
{
//    //手机号以13， 15，18开头，八个 \d 数字字符
//    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//    //    NSLog(@"phoneTest is %@",phoneTest);
//    return [phoneTest evaluateWithObject:mobile];
    
    if ([mobile length] != 11)
    {
        return NO;
    }
    return YES;
}


//邮件格式
+(BOOL)isValidateEmail: (NSString *) candidate
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:candidate];
}

+ (int)getLunBoViewHeight
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 113;
        }
        else
        {
            return  155;
        }
    }
    else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 206;
    }
    else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 225;
    }
    else
        return 225;
}

+ (int)getMyselfLunBoViewHeight
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 115;
        }
        else
        {
            return 122;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 208;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 227;
    }
    else
    {
        return 227;
    }
}

+ (int)getMyselfHeadImageWidth
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 75;
        }
        else
        {
            return 75;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 75;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 75;
    }
    else
    {
        return 75;
    }
}

+ (int)getMyselfHeadImageHeight
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 75;
        }
        else
        {
            return 75;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 75;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 75;
    }
    else
    {
        return 75;
    }
}

+ (int)getMyselfHeadImageTopY
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 10;
        }
        else
        {
            return 10;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 35;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 35;
    }
    else
    {
        return 35;
    }
}

+ (int)getMyselfMidiIdNotLoginY
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return -5;
        }
        else
        {
            return -5;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 5;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 20;
    }
    else
    {
        return 20;
    }
}

+ (int)getMyselfLevelY
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 5;
        }
        else
        {
            return 5;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 15;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 20;
    }
    else
    {
        return 20;
    }
}

+ (int)getMyselfMidiIdAlreadyLoginY
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 5;
        }
        else
        {
            return 3;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 15;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 20;
    }
    else
    {
        return 20;
    }
}

+ (int)getMyselfHeadCellHeight
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 142;
        }
        else
        {
            return 142;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 142;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 142;
    }
    else
    {
        return 142;
    }
}

+ (BOOL)isConnectionAvailable
{
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus])
    {
        case NotReachable:
            isExistenceNetwork = NO;
            //NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //NSLog(@"3G");
            break;
    }
    
    return isExistenceNetwork;
}

+ (NSString *)getMusicBaseLevel:(NSString *)level
{
    if ([level isEqualToString:@"1"])
    {
        return @"初级";
    }
    else if ([level isEqualToString:@"2"])
    {
        return @"中级";
    }
    else if ([level isEqualToString:@"3"])
    {
        return @"高级";
    }
    else
        return @"";
}


@end




























